#include "cBillboard.h"

cEffectPtr cBillboard::shader;

cBillboard::cBillboard(const std::wstring & FileName)
{
	sprite = ShiftEngine::utils::createPlane(cGraphicsEngine::GetInstance().GetDevicePointer());
	
	if(shader.GetData() == nullptr)
	{
		std::vector<InputElement> input;
		input.push_back(InputElement("POSITION", 3));
		input.push_back(InputElement("TEXCOORD", 2));
		input.push_back(InputElement("NORMAL", 3));
		shader = cGraphicsEngine::GetInstance().LoadShader(L"Billboard.fx", input);
	}
	texture = cGraphicsEngine::GetInstance().LoadTexture(FileName);
}

cBillboard::~cBillboard() {}

void cBillboard::Draw( D3DXVECTOR3 camPos, D3DXVECTOR3 angles )
{
	//rotate billboard
	sprite.SetRotation(-angles);

	//draw it like a sprite
	D3DXMATRIX matWorld = sprite.GetWorldMatrix();
	D3DXMATRIX matView = *cGraphicsEngine::GetInstance().GetMatrixPointer(MatrixView);
	D3DXMATRIX matProjection = *cGraphicsEngine::GetInstance().GetMatrixPointer(MatrixProjection);

	cGraphicsEngine::GetInstance().BindShader(shader);
	cGraphicsEngine::GetInstance().SetMatrixConstant("World", &matWorld);
	cGraphicsEngine::GetInstance().SetMatrixConstant("View", &matView);
	cGraphicsEngine::GetInstance().SetMatrixConstant("Projection", &matProjection);
	cGraphicsEngine::GetInstance().BindTexture(texture, "Texture");
	shader.GetData()->Pass->Apply(0);
	sprite.Draw(cGraphicsEngine::GetInstance().GetDevicePointer());
}
